package com.discovery.core.annotation.json;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

/**
 * @Author xuebinlu
 * @Date 2018/5/9 11:25
 * @Version 1.0
 * @Description 具体处理Json过滤逻辑
 **/
public class JsonFilterSerializer {
    private static final String DYNC_INCLUDE = "DYNC_INCLUDE";//包含的标识
    private static final String DYNC_EXCLUDE = "DYNC_EXCLUDE";//过滤的标识
    private ObjectMapper mapper = new ObjectMapper();
    @JsonFilter(DYNC_EXCLUDE)
    interface DynamicExclude{

    }
    @JsonFilter(DYNC_INCLUDE)
    interface DynamicInclude{

    }

    public void filter(JSON json){
        filter(json.type(),json.include(),json.exclude());
    }

    public void filter(Class<?> clazz , String include , String exclude) {
        if (clazz == null) return;
        if (include != null && include.length() > 0) {//包含的操作
            mapper.setFilterProvider(new SimpleFilterProvider()
                    .addFilter(DYNC_INCLUDE, SimpleBeanPropertyFilter.filterOutAllExcept(include.split(","))));//多个字段用,分割开
            mapper.addMixIn(clazz, DynamicInclude.class);
        } else if (exclude != null && exclude.length() > 0) {//过滤的操作
            mapper.setFilterProvider(new SimpleFilterProvider()
                    .addFilter(DYNC_EXCLUDE, SimpleBeanPropertyFilter.serializeAllExcept(exclude.split(","))));
            mapper.addMixIn(clazz, DynamicExclude.class);
        }
    }

    public String toJson(Object object) throws JsonProcessingException {
        return mapper.writeValueAsString(object);
    }

    @Test
    public void testFilterJson() throws JsonProcessingException{
        JsonFilterSerializer jsonFilter = new JsonFilterSerializer();
        jsonFilter.filter(HashMap.class, "status", null);//Response中有status、failReason、currentIndex三个属性
        HashMap<String ,String> tempMap = new HashMap<>();
        tempMap.put("status","失败了");
        tempMap.put("failReason",null);
        tempMap.put("currentIndex","-1");
        System.out.println(jsonFilter.toJson(tempMap));
    }
}
